// 导入模块 Import modules
import { DamageTypes } from './enum/damageTypes';
import { AnimalManager } from './manager/animal';
import { PlayerManager } from './manager/player';

// 设置世界基础属性 Set the basic attribute of the world
world.useOBB = true;

// 玩家进入地图 Player join world
world.onPlayerJoin(({ entity }) => {
    entity.enableDamage = true;
    entity.onDie(() => {
        entity.player.forceRespawn();
    });
    entity.onFluidEnter(() => {
        entity.hurt(100, { damageType: DamageTypes.Drown });
        entity.player.directMessage('你溺死了');
    });
});

// 小红帽任务 Red hat task
const DoorRedHat = world.querySelector('#小红帽-小木屋门');
if (DoorRedHat) {
    DoorRedHat.enableInteract = true;
    DoorRedHat.interactHint = '门';
    DoorRedHat.interactRadius = 2;
    DoorRedHat.onInteract(({ entity }) => {
        if (
            PlayerManager.create(entity).getData((data) => data.action) ===
                '小红帽-搜索小木屋！' ||
            PlayerManager.create(entity)
                .getData((data) => data.Open)
                .includes('小红帽-小木屋的门')
        ) {
            entity.position.set(121, 9, 124);
            PlayerManager.create(entity).setData((data) => {
                if (
                    !PlayerManager.create(entity)
                        .getData((data) => data.Open)
                        .includes('小红帽-小木屋的门')
                ) {
                    data.Open.push('小红帽-小木屋的门');
                }
            });
        } else {
            entity.player.directMessage('你打不开这扇门');
        }
    });
}

// 屏障 Barrier
world.addCollisionFilter('player', '.屏障');

// 树木 Tree
world.querySelectorAll('*').forEach((entity) => {
    if (entity.player || entity.isPlayer) {
        return;
    }
    if (entity.id.startsWith('哈哈小镇-景观树')) {
        entity.collides = false;
        const { position } = entity;
        for (let { y } = position; y >= 0; y--) {
            voxels.setVoxel(position.x, y, position.z, 'barrier');
        }
    }
});

// 小猪 Pig
world.querySelectorAll('.猪').forEach((entity) => {
    AnimalManager.create(entity, '哼~', 'audio/pig.mp3', undefined, 4, 0.25);
});
